package com.f1ashback.bus.controller;

import com.f1ashback.bus.Qo.BusQo;
import com.f1ashback.bus.Vo.BusGet;
import com.f1ashback.bus.Vo.BusVo;
import com.f1ashback.bus.common.AjaxResult;
import com.f1ashback.bus.entity.Bus;
import com.f1ashback.bus.service.BusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/bus")
public class BusController {

    @Autowired
    private BusService busService;

    /**
     * 展示所有公交
     * @return
     */
    @GetMapping("/list")
    public AjaxResult listBus(){
        Map<String,List<BusVo>> res = new HashMap<>();
        List<BusVo> buses = busService.listBus();
        res.put("res",buses);
        if (buses.isEmpty()){
            return AjaxResult.error("未获取到数据！");
        }
        return AjaxResult.success(res);
    }

    @GetMapping("/listBusNo/{id}")
    public AjaxResult listBusNo(@PathVariable Long busId){
        List<String> busNo = busService.listBusNo(busId);
        return AjaxResult.success(busNo);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody BusGet bus){
        return busService.add(bus);
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody BusGet bus){
         return busService.update(bus);
    }

    @DeleteMapping("/del/{busId}")
    public AjaxResult delete(@PathVariable Long busId){
        try {
            busService.delete(busId);
        }catch (Exception e){
            return AjaxResult.error("删除失败！");
        }
        return AjaxResult.success("删除成功！");
    }

    @GetMapping("/listBus")
    public List<BusVo> appListBus(){
        return busService.applistBus();
    }

    @GetMapping("/getBusInfo")
    public List<BusVo> getBusInfoByBusNo(String busNo){
        return busService.getBusInfoByBusNo(busNo);
    }

    @GetMapping("/getBusIdAndNo")
    public AjaxResult getBusIdAndNo(){
        return busService.getBusIdAndNo();
    }

    @PostMapping("/queryBus")
    public AjaxResult queryBus(@RequestBody BusQo busQo){
        return busService.queryBus(busQo);
    }
}
